<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Allocate_Model extends CI_Model
{
    private $tradeGoodsList;
    private $aflag = false;
    private $lflag = false;
    
    private $orderByPro;

    public function getInvoiceByFlagId($flagId)
    {
        //查询满足条件的订单
        $tradeList = $this->getTradeByFlagId($flagId);
        //满足条件的订单写入文件
        $string = serialize($tradeList);
        write_file('./tmp/tradeList.php', $string);
        
        //查询订单商品
        foreach ($tradeList as $key => $value)
        {
            //var_dump($value->TradeID);exit;
            //查询订单商品信息
            $tradeGoods = $this->getTradeGoodsListByTradeId($value->TradeID);
            //var_dump($tradeGoods);exit;
            if(empty ($this->tradeGoodsList) && $this->lflag == FALSE)
            {
                $this->lflag = TRUE;
                $this->tradeGoodsList = $tradeGoods;
            }
            if(!$this->lflag)
            {
                foreach ($tradeGoods as $k => $v)
                {
                    foreach ($this->tradeGoodsList as $ak => $av)
                    {
                        if($v->GoodsID == $av->GoodsID && $v->SpecID == $av->SpecID)
                        {
                            $this->aflag = TRUE;
                            $av->SellCount = $av->SellCount + $v->SellCount;
                            break;
                        }
                    }
                    if(!$this->aflag)
                    {
                        $this->tradeGoodsList[$ak+1] = $tradeGoods[$k];
                    }
                    $this->aflag = FALSE;
                }
            }
            else
            {
                $this->lflag = FALSE;
            }
        }
        //var_dump($this->_step($this->tradeGoodsList));exit;
        //return $this->_step($this->tradeGoodsList);
        
        //var_dump($this->tradeGoodsList);exit;
        if(!empty ($this->tradeGoodsList))
        {
            $this->tradeGoodsList = $this->_step($this->tradeGoodsList);
        }
        $string = serialize($this->tradeGoodsList);
        write_file('./tmp/tradeGoodsList.php', $string); 
        return $this->tradeGoodsList;
    }
    
    private function _step($data)
    {
        foreach ($data as $k => $v)
        {
            //查询商品编号和名字
            $this->db->select('GoodsNO, GoodsName')->from('G_Goods_GoodsList')->where('GoodsID', $v->GoodsID);
            $query = $this->db->get();
            $result = $query->result();
            $data[$k]->GoodsNO = $result[0]->GoodsNO;
            $data[$k]->GoodsName = $result[0]->GoodsName;
            
            //查询商品规格
            if($data[$k]->SpecID != 0)
            {
                $this->db->select('SpecName')->from('G_Goods_GoodsSpec')->where('SpecID', $v->SpecID);
                $query = $this->db->get();
                $result = $query->result();
                $data[$k]->SpecName = $result[0]->SpecName;
            }
            else
            {
                $data[$k]->SpecName = '';
            }
            
            //查询供应商信息
            $this->db->select('ProviderID, Price, GoodsNO')->from('G_Goods_Provider')->where(array('GoodsID'=>$v->GoodsID, 'bDefault'=>True));
            $query = $this->db->get();
            $result = $query->result();
            if(!empty ($result))
            {
                $data[$k]->ProviderID = $result[0]->ProviderID;
                $data[$k]->Price = $result[0]->Price;
                $data[$k]->ProviderGoodsNO = $result[0]->GoodsNO;
            }
            else
            {
                $data[$k]->ProviderID = '';
                $data[$k]->Price = '';
                $data[$k]->ProviderGoodsNO = '';
            }
            
            if($data[$k]->ProviderID != '')
            {
                $this->db->select('ProviderName')->from('G_Cfg_ProviderList')->where('ProviderID', $data[$k]->ProviderID);
                $query = $this->db->get();
                $result = $query->result();
                $data[$k]->ProviderName = $result[0]->ProviderName;
            }
            else
            {
                $data[$k]->ProviderName = '';
            }
            
            //查询实际库存和警戒库存
            $this->db->select('RealStock, RealWarStock')->from('G_Stock_Spec')->where(array('GoodsID'=>$v->GoodsID, 'SpecID'=>$v->SpecID));
            $query = $this->db->get();
            $result = $query->result();
            if($result[0]->RealStock == NULL || $result[0]->RealStock == '')
            {
                $data[$k]->RealStock = 0;
            }
            else
            {
                $data[$k]->RealStock = $result[0]->RealStock;
            }
            if($result[0]->RealWarStock == NULL || $result[0]->RealWarStock == '')
            {
                $data[$k]->RealWarStock = 0;
            }
            else
            {
                $data[$k]->RealWarStock = $result[0]->RealWarStock;
            }
            
            //编码转换
            $data[$k]->TradeSpec = iconv('GB2312', 'UTF-8', $data[$k]->TradeSpec);
            $data[$k]->TradeName = iconv('GB2312', 'UTF-8', $data[$k]->TradeName);
            $data[$k]->ProviderGoodsNO = iconv('GB2312', 'UTF-8', $data[$k]->ProviderGoodsNO);
            $data[$k]->ProviderName = iconv('GB2312', 'UTF-8', $data[$k]->ProviderName);
            $data[$k]->GoodsName = iconv('GB2312', 'UTF-8', $data[$k]->GoodsName);
            $data[$k]->SpecName = iconv('GB2312', 'UTF-8', $data[$k]->SpecName);
            
            //排序条件 商品编号
            //$order[$k] = $data[$k]->TradeGoodsNO;
            $order[$k] = $data[$k]->ProviderName;
            //$this->orderByPro[$k] = $data[$k]->ProviderName;
            
            //统计
            if($v->RealStock - $v->SellCount >= $v->RealWarStock)
            {
                //需要进货的数量
                $data[$k]->marketNum = 0;
                //仓库拿出的库存
                $data[$k]->stockOut = $v->SellCount;
                //需要写入数据库的剩余库存
                $data[$k]->surpStockNum = $v->RealStock - $v->SellCount;
            }
            elseif($v->RealStock <= $v->RealWarStock)
            {
                //需要进货的数量等于卖出数量
                $data[$k]->marketNum = $v->SellCount;
                //仓库拿出的库存
                $data[$k]->stockOut = 0;
                //如果需要写入的实际库存为-1，则跳过，不写入。
                $data[$k]->surpStockNum = -1;
            }
            else
            {
                //仓库拿出的库存
                $data[$k]->stockOut = $v->RealStock - $v->RealWarStock;
                //需要进货的数量
                $data[$k]->marketNum = $v->SellCount - $data[$k]->stockOut;
                //写入数据库的剩余库存
                $data[$k]->surpStockNum = $v->RealWarStock;
            }
            
            if($data[$k]->ProviderName != '')
            {
                $data[$k]->ProviderNameGro = mb_substr($data[$k]->ProviderName, 0, 1, 'UTF-8');
                if($data[$k]->ProviderNameGro == 'D')
                {
                    $data[$k]->ProviderNameGro = mb_substr($data[$k]->ProviderName, 0, 2, 'UTF-8');
                }
            }
            else
            {
                $data[$k]->ProviderNameGro = '';
            }
            //设置仓库是否出货标志
            if($data[$k]->stockOut > 0)
            {
                if($data[$k]->marketNum >0)
                {
                    //仓库和市场都需要出货
                    $data[$k]->stockOutflag = 1;
                }
                else
                {
                    //市场不需出货，仅仓库需要出货
                    $data[$k]->stockOutflag = 2;
                }
                //$data[$k]->stockOutflag = TRUE;
            }
            else
            {
                //仓库不需要出货，仅市场需要出货
                $data[$k]->stockOutflag = 0;
            }
        }
        
        //排序
        if(is_array($data))
        {
            array_multisort($order, $data);
        }
        
        return $data;
    }

    public function getTradeByFlagId($tradeFlagId)
    {
        $this->db->select('TradeID')->from('G_Trade_TradeList')->where(array('TradeStatus'=>5, 'TradeFlagID'=>$tradeFlagId));
        $query = $this->db->get();
        return $query->result();
    }
    
    public function getTradeGoodsListByTradeId($tradeId)
    {
        $this->db->select('GoodsID, SpecID, SellCount, TradeSpec, TradeGoodsNO, TradeName');
        $query = $this->db->get_where('G_Trade_GoodsList', array('TradeID' => $tradeId));
        return $query->result();
    }
    
    public function updateFlagInDataBase($tradeList)
    {
        foreach ($tradeList as $k => $v)
        {
            $data = array(
                'TradeFlagID' => 21
            );
            $this->db->where('TradeID', $v->TradeID);
            $this->db->update('G_Trade_TradeList', $data); 
        }
    }

    public function updateData($tradeGoodsList)
    {
        if(!empty ($tradeGoodsList))
        {
            foreach ($tradeGoodsList as $k => $v)
            {
                if($v->surpStockNum > -1)
                {
                    $data = array(
                               'RealStock' => $v->surpStockNum
                            );
                    $this->db->where(array('GoodsID'=>$v->GoodsID, 'SpecID'=>$v->SpecID));
                    $this->db->update('G_Stock_Spec', $data); 
                }
            }
        }
        else
        {
            return false;
        }
    }
}
